/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sound;

//import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintStream;
import java.util.Map;
import javax.sound.sampled.AudioInputStream;
import java.io.InputStream;

import sound.music.javazoom.jlgui.basicplayer.*;
import util.LoopingByteInputStream;

/**
 *
 * @author Yati
 */
public class MusicManager implements BasicPlayerListener{
    
    BasicController control;
    BasicPlayer player;
    boolean loopCurrent;
    String currMusic=null;
    long elapsedtime;
    
    public MusicManager(){
            
        player=new BasicPlayer();
        control=(BasicController) player;
        player.addBasicPlayerListener(this);
        loopCurrent=false;
        elapsedtime=0;
        
    }
    
    public void play(String filename,boolean looping){
        
        //loopCurrent=false;
        elapsedtime=0;
        try{
        control.open(new File(filename));
        control.play();
        currMusic=filename;
        control.setGain(0.85);
        //control.setPan(0);
        loopCurrent=looping;
        
        }catch(BasicPlayerException e){
            e.printStackTrace();
        }
        
    }
    
    public void play(InputStream music,boolean looping){
        try{
        control.open(music);
        control.play();
        control.setGain(0.0);
        loopCurrent=looping;
        //currMusic=music;
        }catch(BasicPlayerException e){
            e.printStackTrace();
        }
    }
    
    public void play(Sound music,boolean looping){
        try{
            if(looping){
                control.open(new LoopingByteInputStream(music.getSamples()));
            }else{
            control.open(new ByteArrayInputStream(music.getSamples()));
            }
            control.play();
            control.setGain(0.85);
            control.setPan(0);
            loopCurrent=looping;
        }catch(BasicPlayerException e){
            e.printStackTrace();
        }
        
        
    }
    
    public void open(String filename){
        try{
        control.open(new File(filename));
        }catch(BasicPlayerException e){
            e.printStackTrace();
        }
    }
    
    public void opened(Object o, Map properties){
        System.out.println("opened : "+properties.toString());	
    }

    public void progress(int i, long l, byte[] bytes, Map properties){
        System.out.println("progress : "+properties.toString());
        this.elapsedtime=l;
    }

    public void stateUpdated(BasicPlayerEvent bpe){
        System.out.println("stateUpdated : "+bpe.toString());
        if(bpe.getCode()==BasicPlayerEvent.EOM){
            if(loopCurrent){
                try{
                    control.stop();
                }catch(BasicPlayerException e){
                    e.printStackTrace();
                }
                play(currMusic, loopCurrent);
            }else{
                try{
                    control.stop();
                }catch(BasicPlayerException e){
                    e.printStackTrace();
                }
            }
        }
        if(bpe.getCode()==BasicPlayerEvent.STOPPED){
                try{
                    control.stop();
                }catch(BasicPlayerException e){
                    e.printStackTrace();
                }
        }
        
    }
    
    
    
    public void setController(BasicController bc){}
    
    public void stop(){
        try{
        control.stop();
        }catch(BasicPlayerException e){
            e.printStackTrace();
        }
    
    }
    
    public void pause(){
        try{
        control.pause();
        }catch(BasicPlayerException e){
            e.printStackTrace();
        }
        
    }
    
    public void resume(){
        try{
            control.resume();
        }catch(BasicPlayerException e){
            e.printStackTrace();
        }
    }
    
    
}
